package com.tsx.ai.chat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsx.ai.chat.properties.SessionProperties;
import com.tsx.ai.chat.entity.ChatSession;
import com.tsx.ai.chat.mapper.ChatSessionMapper;
import com.tsx.ai.chat.service.ChatSessionService;
import com.tsx.ai.chat.vo.SessionVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sx.tong
 * @description 针对表【chat_session(对话session)】的数据库操作Service实现
 * @createDate 2025-07-15 16:12:40
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ChatSessionServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession>
        implements ChatSessionService {
    private final SessionProperties sessionProperties;
    private final ChatSessionMapper chatSessionMapper;

    @Override
    public SessionVO createSession(Integer num) {
        SessionVO sessionVO = BeanUtil.toBean(sessionProperties, SessionVO.class);
        // 随机获取examples
        List<SessionVO.Example> randomEleList = RandomUtil.randomEleList(sessionProperties.getExamples(), num);
        sessionVO.setExamples(randomEleList);
        sessionVO.setSessionId(IdUtil.fastSimpleUUID());
        // 持久化session
        ChatSession chatSession = new ChatSession();
        chatSession.setSessionId(sessionVO.getSessionId());
        chatSession.setUserId(1L); // 定死
        chatSessionMapper.insert(chatSession);
        return sessionVO;
    }

    @Override
    public List<SessionVO.Example> hotExamples(Integer num) {
        return RandomUtil.randomEleList(sessionProperties.getExamples(), num);
    }
}




